package com.yssoft.controller.app.equipment;

import com.yssoft.entity.core.TCode;
import com.yssoft.entity.core.TDdDetail;
import com.yssoft.entity.equipment.TEquipmentImgP;
import com.yssoft.entity.equipment.TEquipmentP;
import com.yssoft.entity.inventory.TWarehouse;
import com.yssoft.service.core.SupplierService;
import com.yssoft.service.equipment.EquipmentPService;
import com.yssoft.service.inventory.WarehouseService;
import com.yssoft.utils.AppUtil;

import org.apache.commons.codec.DecoderException;
import org.apache.log4j.Logger;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @类名称: EquipmentPController
 * @类描述: 生产设备控制器
 * @作者: 于兴达
 * @创建时间:2018年9月7日 下午2:36:50
 */
@CrossOrigin//解决跨域session问题
@Controller
@RequestMapping("/app/equipmentp")
public class EquipmentPController {
	private static Logger logger=Logger.getLogger(SupplierService.class);
    @Resource
    private EquipmentPService equipmentPService;
    
    /**
     * @方法名称: getWarehouseList
     * @功能描述: 获取生产设备列表
     * @作者: 于兴达
     * @创建时间:2018年9月7日 下午2:33:38
     * @param companyId 企业id
     * @param name 生产设备名
     * @return Object
     */
    @RequestMapping(value = "getEquipmentPList",method = RequestMethod.POST)
    @ResponseBody
    public Object getWarehouseList(int page, int limit,Integer companyId,String name) {
    	 if (null == companyId) {
             return AppUtil.returnMapJson("0", "查询失败，请传递企业id", null);
         }
         try {
             List<Map<String, Object>> warehouseList = equipmentPService.findAll(page,limit,companyId,name);
             return AppUtil.returnListJson("1", "查询成功!", warehouseList);
         } catch (NumberFormatException e) {
             return AppUtil.returnMapJson("0", "查询失败!"+e.getMessage(), null);
         }
    }
    
    /**
     * @方法名称: getOne
     * @功能描述: 根据生产设备id查询生产设备
     * @作者: 于兴达
     * @创建时间:2018年9月7日 下午19:03:38
     * @param id 仓库id
     * @return Object
     */
    @RequestMapping(value = "getOne",method = RequestMethod.GET)
    @ResponseBody
    public Object getOne(String id) {
         try {
             Map<String, Object> equipmentp = equipmentPService.getOne(id);
             return AppUtil.returnMapJson("1", "查询成功!", equipmentp);
         } catch (NumberFormatException e) {
             return AppUtil.returnMapJson("0", "查询失败!"+e.getMessage(), null);
         }
    }
    
    /**
     * @方法名称: saveEquipmentP
     * @功能描述: 保存生产设备
     * @作者: 于兴达
     * @创建时间:2018年9月7日 下午19:04:38
     * @param equipmentP 生产设备实体
     * @param id 生产线id
     * @return Object
     */
    @RequestMapping(value = "saveEquipmentP",method = RequestMethod.POST)
    @ResponseBody
    public Object saveEquipmentP(TEquipmentP equipmentP,String ids,@RequestParam("files") MultipartFile[] files,HttpServletRequest request)throws IOException, DecoderException {
         try {
        	equipmentPService.saveEquipmentP(equipmentP,ids,files,request);
             return AppUtil.returnMapJson("1", "保存成功!", null);
         } catch (NumberFormatException e) {
             return AppUtil.returnMapJson("0", "保存失败!"+e.getMessage(), null);
         }
    }
    /**
     * @方法名称: deleteEquipmentP
     * @功能描述: 删除生产设备
     * @作者: 于兴达
     * @创建时间:2018年9月7日 下午19:04:38
     * @param id 设备id
     * @return Object
     */
    @RequestMapping(value = "deleteEquipmentP",method = RequestMethod.POST)
    @ResponseBody
    public Object deleteEquipmentP(String id) {
         try {
        	 equipmentPService.deleteEquipmentP(id);
             return AppUtil.returnMapJson("1", "删除成功!", null);
         } catch (NumberFormatException e) {
             return AppUtil.returnMapJson("0", "删除失败!"+e.getMessage(), null);
         }
    }
    /**
   	 * 图片上传
   	 * @param request
   	 * @return
   	 * @throws Exception
   	 */
   	@ResponseBody
   	@RequestMapping(value = "uploadImage", method = RequestMethod.POST)
   	public Object uploadImage(HttpServletRequest request) throws Exception {
   		try{
   			MultipartHttpServletRequest mRequest = (MultipartHttpServletRequest) request;
   			MultipartFile mFile = mRequest.getFile("img");
   			Map<String, Object> fileNames = equipmentPService.compressUploadedFile(mFile);
   			return AppUtil.returnMapJson("1", "上传成功!", fileNames);
   		} catch (Exception e) {
   			logger.info("上传错误:"+e.getMessage());
   			return AppUtil.returnMapJson("0", "上传失败!", null);
   		}
   	}
   	/**
     * @方法名称: getWarehouseListck
     * @功能描述: 获取生产产线
     * @作者: 于兴达
     * @创建时间:2018年9月08日 上午10:56:38
     * @param param 生产产线参数
     * @return Object
     */
	@RequestMapping(value = "getProductionLine",method = RequestMethod.GET)
    @ResponseBody
    public Object getWarehouseType(Integer companyId,String id) {
         try {
             List<Map<String, Object>> productionLineList = equipmentPService.findProductionLine(companyId,id);
			return AppUtil.returnListJson("1", "查询成功!", productionLineList);
         } catch (NumberFormatException e) {
             return AppUtil.returnMapJson("0", "查询失败!"+e.getMessage(), null);
         }
    }
	/**
     * @方法名称: deleteImg
     * @功能描述: 删除图片
     * @作者: 于兴达
     * @创建时间:2018年9月08日 上午11:35:38
     * @return Map<String, Object>
     */
	@RequestMapping(value = "deleteImg",method = RequestMethod.POST)
    @ResponseBody
    public Object deleteImg(String id) {
         try {
        	 equipmentPService.deleteImg(id);
             return AppUtil.returnMapJson("1", "删除成功!", null);
         } catch (NumberFormatException e) {
             return AppUtil.returnMapJson("0", "删除失败!"+e.getMessage(), null);
         }
    }
}
